﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Sale_Operation;
using Sale_Common;
using Sale_Model;
using SaleSystem;
using ExtAspNet;


namespace SaleSystem.BaseData
{
    public partial class frm_SupplierEdit : PageBase
    {
        #region Page Event
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitControl(); 
                GetAndBind();
            }
        }
        protected void bnSave_Click(object sender, EventArgs e)
        {
            SModel.CreateMan = LinkService.GetCurrentUser().SUser_Id;
            SModel.CreateTime = System.DateTime.Now;            
            SModel.State = 1;
            SModel.Supp_Code = this.tb_Supp_Code.Text;
            SModel.Supp_Id = Id;
            SModel.Supp_LinkMan1 = this.tb_Supp_LinkMan1.Text;
            SModel.Supp_LinkMan2 = this.tb_Supp_LinkMan2.Text;
            SModel.Supp_Memo = this.tb_Supp_Memo.Text;
            SModel.Supp_Name = this.tb_Supp_Name.Text;
            SModel.Supp_Tel1 = this.tb_Supp_Tel1.Text;
            SModel.Supp_Tel2 = this.tb_Supp_Tel2.Text;
            bool blHasExists = Manager.HasExists(SModel);
            if (blHasExists)
            {
                ExtAspNet.Alert.Show("编码或者名称已经存在，请重新输入！", MessageBoxIcon.Information);
            }
            else
            {
                bool blRes = Manager.Save(SModel);
                if (blRes)
                    ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.GetHidePostBackReference(LinkService.RefreshString));

                else
                    ExtAspNet.Alert.Show("客户信息编辑失败！", MessageBoxIcon.Error);
            }
        }
        #endregion

        #region Method
        private void InitControl()
        {
            bnClose.OnClientClick = ExtAspNet.ActiveWindow.GetHideReference();
            if (Request["Id"] != null)
            {
                Id = ValueHandler.GetIntNumberValue(Request["Id"]);
            }
        } 
        private void GetAndBind()
        {
            if (Id != 0)
            {
                SModel.Supp_Id = Id;
                SModel = Manager.GetModel(SModel) as Supplier;
                this.tb_Supp_Code.Text = SModel.Supp_Code;
                this.tb_Supp_LinkMan1.Text = SModel.Supp_LinkMan1;
                this.tb_Supp_LinkMan2.Text = SModel.Supp_LinkMan2;
                this.tb_Supp_Memo.Text = SModel.Supp_Memo;
                this.tb_Supp_Name.Text = SModel.Supp_Name;
                this.tb_Supp_Tel1.Text = SModel.Supp_Tel1;
                this.tb_Supp_Tel2.Text = SModel.Supp_Tel2;                
            }
            else
            {
                this.tb_Supp_Code.Text = Coder.CreateCoder(EMenuList.供应商管理);
            }
        }

        #endregion

        #region Property
        SupplierOp Manager = new SupplierOp();
        new Sale_Model.Supplier SModel = new Sale_Model.Supplier();
        #endregion
    }
}
